home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqsys / generic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-15  |  4.5 KB  |  246 lines

  1. #define    LIBQSYS_CORE
  2. #define    LIBQTOOLS_CORE
  3. #include "../include/libqsys.h"
  4. #include "../include/libqtools.h"
  5.  
  6. #ifndef    SWAPSHORT
  7. short SwapShort(short l)
  8. {
  9.   unsigned char b1, b2;
  10.  
  11.   b1 = l & 255;
  12.   b2 = (l >> 8) & 255;
  13.  
  14.   return (b1 << 8) + b2;
  15. }
  16. #endif
  17.  
  18. #ifndef    SWAPLONG
  19. int SwapLong(int l)
  20. {
  21.   unsigned char b1, b2, b3, b4;
  22.  
  23.   b1 = l & 255;
  24.   b2 = (l >> 8) & 255;
  25.   b3 = (l >> 16) & 255;
  26.   b4 = (l >> 24) & 255;
  27.  
  28.   return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  29. }
  30.  
  31. #endif
  32.  
  33. #ifndef    SWAPFLOAT
  34. float SwapFloat(float l)
  35. {
  36.   union {
  37.     unsigned char b[4];
  38.     float f;
  39.   } in, out;
  40.  
  41.   in.f = l;
  42.   out.b[0] = in.b[3];
  43.   out.b[1] = in.b[2];
  44.   out.b[2] = in.b[1];
  45.   out.b[3] = in.b[0];
  46.  
  47.   return out.f;
  48. }
  49. #endif
  50.  
  51. /* this is an offscreen-renderer */
  52. #ifndef    OPENDISPLAY
  53. struct DisplayDimension rootDim =
  54. {0};                                /* empty root at the beginning */
  55.  
  56. struct DisplayDimension *OpenDisplay(short int width, short int height, char depth, struct rgb *Palette)
  57. {
  58.   struct DisplayDimension *newDim;
  59.  
  60.   if ((newDim = (struct DisplayDimension *)tmalloc(sizeof(struct DisplayDimension)))) {
  61.     newDim->changedOffset = TRUE;
  62.     newDim->X = 0;
  63.     newDim->Y = 0;
  64.     newDim->changedSize = TRUE;
  65.     newDim->Width = width;
  66.     newDim->Height = height;
  67.     newDim->changedDesktopOffset = TRUE;
  68.     newDim->dtX = 0;
  69.     newDim->dtY = 0;
  70.     newDim->changedDesktopSize = TRUE;
  71.     newDim->dtWidth = width;
  72.     newDim->dtHeight = height;
  73.     newDim->changedBuffer = TRUE;
  74.     newDim->frameDepth = depth;
  75.     newDim->frameSize = width * height;
  76.     newDim->ID = rootDim.ID;
  77.     newDim->nextDim = rootDim.nextDim;
  78.     newDim->driverPrivate = 0;
  79.  
  80.     rootDim.nextDim = newDim;
  81.     rootDim.ID++;
  82.  
  83.     if (depth <= 8)
  84.       newDim->frameBPP = 1;                    /* index based */
  85.     else if (depth <= 16)
  86.       newDim->frameBPP = 2;                    /* 15/16 bit rgb */
  87.     else if (depth <= 24)
  88.       newDim->frameBPP = 3;                    /* 24 bit rgb */
  89.     else
  90.       newDim->frameBPP = 4;                    /* 32 bit argb */
  91.  
  92.     if (!(newDim->frameBuffer = (void *)tmalloc(width * height * newDim->frameBPP)))
  93.       Error(failed_memoryunsize, "offscreen framebuffer");
  94. #ifdef    USE_ZBUFFER
  95.     if (!(newDim->zBuffer = (unsigned short int *)tmalloc(width * height * sizeof(unsigned short int))))
  96.         Error(failed_memoryunsize, "offscreen zbuffer");
  97.  
  98. #endif
  99.   }
  100.  
  101.   return newDim;
  102. }
  103. #endif
  104.  
  105. /* this swaps buffer if doublebuffer */
  106. #ifndef    SWAPDISPLAY
  107. void *SwapDisplay(void *oldBuffer)
  108. {
  109.   return oldBuffer;
  110. }
  111. #endif
  112.  
  113. #ifndef    UPDATEDISPLAY
  114. void *UpdateDisplay(void *oldBuffer, short int x, short int y, short int width, short int height)
  115. {
  116.   return oldBuffer;
  117. }
  118. #endif
  119.  
  120. #ifndef    CHANGEDISPLAY
  121. struct DisplayDimension *ChangeDisplay(short int width, short int height, char depth, struct rgb *Palette, char *Title)
  122. {
  123.   return 0;
  124. }
  125. #endif
  126.  
  127. /*
  128.  * 
  129.  */
  130. #ifndef    CLOSEDISPLAY
  131. void CloseDisplay(void)
  132. {
  133. }
  134.  
  135. #endif
  136.  
  137. /*
  138.  * 
  139.  */
  140. #ifndef    OPENKEYS
  141. void OpenKeys(void)
  142. {
  143. }
  144.  
  145. #endif
  146.  
  147. /*
  148.  * the caller repeats through this untill it gets -1 or ESCAPE as a terminator
  149.  * or return FALSE
  150.  */
  151. #ifndef    GETKEYS
  152. bool GetKeys(struct keyEvent *eventBuffer)
  153. {
  154.   eventBuffer->pressed = RAWKEY_ESCAPE;
  155.   return TRUE;
  156. }
  157. #endif
  158.  
  159. /*
  160.  * 
  161.  */
  162. #ifndef    CLOSEKEYS
  163. void CloseKeys(void)
  164. {
  165. }
  166.  
  167. #endif
  168.  
  169. /*
  170.  * 
  171.  */
  172. #ifndef    OPENMOUSE
  173. void OpenMouse(void)
  174. {
  175. }
  176.  
  177. #endif
  178.  
  179. /*
  180.  * the caller repeats through this untill it gets -1
  181.  */
  182. #ifndef    GETMOUSE
  183. void GetMouse(struct mouseEvent *eventBuffer)
  184. {
  185.   eventBuffer->pressed = RAWMOUSE_NOTHING;
  186.   eventBuffer->mouseX = 0;
  187.   eventBuffer->mouseY = 0;
  188. }
  189.  
  190. #endif
  191.  
  192. /*
  193.  * 
  194.  */
  195. #ifndef    CLOSEMOUSE
  196. void CloseMouse(void)
  197. {
  198. }
  199.  
  200. #endif
  201.  
  202. /*
  203.  * this is a generic match
  204.  */
  205. #ifndef    MATCH
  206. unsigned char Match(register struct rgb *rawpix, register struct rgb *Palette)
  207. {
  208.   unsigned char palpix = 0;
  209.   short int i;
  210.   short int match = 0x7FFF;
  211.  
  212.   /* save conversion? has a palette only 6 valid bits? */
  213.   short int rawpixR = (short int)(rawpix->r);
  214.   short int rawpixG = (short int)(rawpix->g);
  215.   short int rawpixB = (short int)(rawpix->b);
  216.  
  217.   /* find match */
  218.   for (i = 0; i < 256; i++) {
  219.     short int R, G, B, thismatch;
  220.  
  221.     if ((R = (short int)(Palette[i].r) - rawpixR) < 0)
  222.       R = -R;
  223.     if ((G = (short int)(Palette[i].g) - rawpixG) < 0)
  224.       G = -G;
  225.     if ((B = (short int)(Palette[i].b) - rawpixB) < 0)
  226.       B = -B;
  227.     if ((thismatch = R + G + B) != 0) {
  228.       if (thismatch < match) {
  229.     match = thismatch;
  230.     palpix = (unsigned char)i;
  231.       }
  232.     }
  233.     else
  234.       return (unsigned char)i;
  235.   }
  236.   return palpix;
  237. }
  238. #endif
  239.  
  240. struct DisplayDimension localDim;
  241.  
  242. void SetDisplay(struct DisplayDimension *dim)
  243. {
  244.   localDim = *dim;
  245. }
  246.